package com.zfei.gfb.controller;

import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbCompanyMapper;
import com.zfei.gfb.model.GfbCompany;
import com.zfei.gfb.request.GfbCompanyEditRequest;
import com.zfei.gfb.request.GfbGetRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.vo.GfbCompanyVo;
import com.zfei.gfb.vo.GfbHouseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author
 * @version 1.0
 */
@Api(tags = "h5企业账号接口")
@RestController
@RequestMapping("/h5/company")
public class H5CompanyController {

    @Resource
    private GfbCompanyMapper gfbCompanyMapper;


    @Auth(type = Const.COMPANY)
    @ApiOperation(value = "企业认证3-3-2")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = GfbHouseVo.class),
    })
    @LogResult
    @HttpJson("/edit")
    public Object edit(@RequestBody GfbCompanyEditRequest request) {

        request.checkUpdateNull();

        GfbCompany record = gfbCompanyMapper.selectByPrimaryKey(request.getId());
        if (record == null) {
            throw new StatusException(Status.NOT_EXIST);
        }

        BeanUtils.copyProperties(request, record);

        record.setAudit(Const.COMPANY_AUTHENTICATING);
        record.setSubmitTime(Now.millis());
        record.setUpdateTime(Now.millis());

        gfbCompanyMapper.updateByPrimaryKeySelective(record);

        GfbCompany result = gfbCompanyMapper.selectByPrimaryKey(request.getId());
        Object vo = Beans.copy(result, GfbHouseVo.class);
        return vo;
    }

    @ApiOperation(value = "详情")
    @ApiResponses({
            @ApiResponse(code = 200, message = "",response = GfbCompanyVo.class),
    })
    @LogResult
    @HttpJson("/get")
    public Object get(@RequestBody GfbGetRequest request) {
        request.checkGetNull();
        Integer id = request.getId();

        GfbCompany record = gfbCompanyMapper.selectByPrimaryKey(id);

        GfbCompanyVo vo = Beans.copy(record, GfbCompanyVo.class);
        return vo;
    }
}
